package io.renren.modules.hospital.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.hospital.entity.EmrJdInfoEntity;
import io.renren.modules.hospital.service.EmrJdInfoService;
import io.renren.modules.hospital.service.EmrXyModelService;
import io.renren.modules.hospital.service.EmrZyModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * @author starry
 * @email 2495646304@qq.com
 * @date 2024-01-29 18:00:41
 */
@RestController
@RequestMapping("hospital/emrjdinfo")
@Api(value = "EMR病历节点接口", tags = "EMR病历节点接口")
public class EmrJdInfoController {
    @Autowired
    private EmrJdInfoService emrJdInfoService;
    @Autowired
    private EmrXyModelService emrXyModelService;
    @Autowired
    private EmrZyModelService emrZyModelService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    //@RequiresPermissions("hospital:emrjdinfo:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = emrJdInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{emrJdId}")
    @ApiOperation("信息")
    //@RequiresPermissions("hospital:emrjdinfo:info")
    public R info(@PathVariable("emrJdId") Integer emrJdId) {
        EmrJdInfoEntity emrJdInfo = emrJdInfoService.getById(emrJdId);

        return R.ok().put("emrJdInfo", emrJdInfo);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("保存节点以及模板信息")
    @Transactional
    public R save(@RequestBody EmrJdInfoEntity emrJdInfo) {
        boolean xyResult = true;
        boolean zyResult = true;
        System.out.println("emrJdInfo.getEmrJdTypeId()" + emrJdInfo.getEmrJdTypeId());
        if (emrJdInfo.getEmrJdTypeId() == 0) {
            xyResult = emrXyModelService.save(emrJdInfo.getModelEntity());
        } else {
            zyResult = emrZyModelService.save(emrJdInfo.getZyModelEntity());
        }
        boolean jdResult = emrJdInfoService.saveMedicalNode(emrJdInfo);

        if (!xyResult) {
            return Objects.requireNonNull(R.error("事务执行失败").put("xy", false)).put("jd", false);
        }
        if (!zyResult) {
            return Objects.requireNonNull(R.error("事务执行失败").put("zy", false)).put("jd", false);
        }

        return R.ok().put("jdPage", emrJdInfo.getEmrJdPage());
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    //@RequiresPermissions("hospital:emrjdinfo:update")
    public R update(@RequestBody EmrJdInfoEntity emrJdInfo) {
        emrJdInfoService.updateById(emrJdInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除")
    //@RequiresPermissions("hospital:emrjdinfo:delete")
    public R delete(@RequestBody Integer[] emrJdIds) {
        emrJdInfoService.removeByIds(Arrays.asList(emrJdIds));

        return R.ok();
    }


    /**
     * 根据病历编号查找其下的西医病历节点
     *
     * @param blId 病历编号
     * @return 病历节点列表
     */
    @GetMapping("/getXyJdByBlId")
    @ApiOperation("根据病历编号查找其下的西医病历节点")
    public R getXyJdByBlId(Integer blId) {
        List<EmrJdInfoEntity> jdInfoEntities = emrJdInfoService.getJdByBlId(blId);
        return R.ok().put("jdList", jdInfoEntities);
    }

    /**
     * 根据病历编号查找其下的中医医病历节点
     *
     * @param blId 病历编号
     * @return 病历节点列表
     */
    @GetMapping("/getZyJdByBlId")
    @ApiOperation("根据病历编号查找其下的中医病历节点")
    public R getZyJdByBlId(Integer blId) {
        List<EmrJdInfoEntity> jdInfoEntities = emrJdInfoService.getZyJdByBlId(blId);
        return R.ok().put("jdList", jdInfoEntities);
    }
}
